//
//  JKLeafProgressView.m
//  JKLeafView
//
//  Created by HelloJingQiu on 16/1/29.
//  Copyright © 2016年 HelloJingQiu. All rights reserved.
//

#import "JKLeafProgressView.h"

@interface JKLeafProgressView ()

@property (strong,nonatomic) UIImageView *flowerView;/**< 叶片图片*/
@property (strong,nonatomic) UIImageView *progressImageView;/**< 进度条图片*/
@property (strong,nonatomic) UIImageView *progressBGImageView;/**< 进度条背景*/
@property (strong,nonatomic) UILabel *textLabel;/**< 文本框文字*/

@end

@implementation JKLeafProgressView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        //创建背景
        UIImageView *bgImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        bgImageView.image = [[UIImage imageNamed:@"bg"] stretchableImageWithLeftCapWidth:124/2.0 topCapHeight:35/2.0];
        [self addSubview:bgImageView];
        
        self.flowerView = [[UIImageView alloc]initWithFrame:CGRectMake(self.frame.size.width - 32, 2, 30, 30)];
        _flowerView.image = [[UIImage imageNamed:@"flower"] stretchableImageWithLeftCapWidth:126/2.0 topCapHeight:119/2.0];
        [self addSubview:_flowerView];

        self.progressBGImageView = [[UIImageView alloc]initWithFrame:CGRectMake(4, 6.2, frame.size.width - 8 - 30, 24)];
        _progressBGImageView.contentMode = UIViewContentModeLeft;
        _progressBGImageView.backgroundColor = [UIColor clearColor];
        _progressBGImageView.clipsToBounds = YES;
        [self addSubview:_progressBGImageView];
        
        self.progressImageView = [[UIImageView alloc]initWithFrame:CGRectMake(4, 6.2, 0, 24)];
        _progressImageView.image = [[UIImage imageNamed:@"progress"] stretchableImageWithLeftCapWidth:86/2.0 topCapHeight:24/2.0];
        _progressImageView.contentMode = UIViewContentModeLeft;
        _progressImageView.clipsToBounds = YES;
        [self addSubview:_progressImageView];
        
        self.textLabel = [[UILabel alloc]initWithFrame:_flowerView.frame];
        _textLabel.text = @"100\%";
        _textLabel.textColor = [UIColor colorWithRed:0.788 green:0.400 blue:1.000 alpha:1.000];
        _textLabel.font = [UIFont boldSystemFontOfSize:11];
        _textLabel.textAlignment = NSTextAlignmentCenter;
//        _textLabel.hidden = YES;
        [self addSubview:_textLabel];
    }
    return self;
}

-(void)startLoading{
    //花瓣转动
    CABasicAnimation *rotateAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];
    rotateAnimation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
    rotateAnimation.toValue = [NSValue valueWithCATransform3D:CATransform3DRotate(_flowerView.layer.transform, M_PI, 0, 0, 1)];
    rotateAnimation.cumulative = YES;
    rotateAnimation.duration = 0.5;
    rotateAnimation.repeatCount = MAXFLOAT;
    
    [self.flowerView.layer addAnimation:rotateAnimation forKey:@"flowerAnimation"];
}

-(void)stopLoading{
    [_flowerView.layer removeAllAnimations];
    
    CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transfrom.scale"];
    scaleAnimation.fromValue = @1;
    scaleAnimation.toValue = @0;
    scaleAnimation.duration = 0.5;
    scaleAnimation.removedOnCompletion = NO;
    scaleAnimation.fillMode = kCAFillModeForwards;
    [self.flowerView.layer addAnimation:scaleAnimation forKey:nil];
    
    _textLabel.hidden = NO;
    scaleAnimation.fromValue = @0;
    scaleAnimation.toValue = @1;
    [_textLabel.layer addAnimation:scaleAnimation forKey:nil];
    
}

-(void)setProgress:(CGFloat)rate{
    _textLabel.text = [NSString stringWithFormat:@"%g%%",rate*100.0];
    
    self.progressImageView.frame = CGRectMake(CGRectGetMinX(_progressImageView.frame), CGRectGetMinY(_progressImageView.frame), CGRectGetWidth(_progressBGImageView.frame) * rate, CGRectGetHeight(_progressImageView.frame));
    
    //树叶效果
    CALayer *leafLayer = [CALayer layer];
    leafLayer.contents = (__bridge id _Nullable)([UIImage imageNamed:@"leaf"].CGImage);
    leafLayer.bounds = CGRectMake(0, 0, 10, 10);
    leafLayer.position = CGPointMake(CGRectGetMinX(_progressBGImageView.frame) + CGRectGetWidth(_progressBGImageView.frame), 8);
    [self.progressBGImageView.layer addSublayer:leafLayer];
    
    //开始树叶的动画
    CAKeyframeAnimation *keyAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    NSValue *p1 = [NSValue valueWithCGPoint:leafLayer.position];;
    NSValue *p2 = [NSValue valueWithCGPoint:CGPointMake(CGRectGetMinX(_progressBGImageView.frame) + CGRectGetWidth(_progressBGImageView.frame) * 3 / 4.0 + arc4random()%(int)(_progressBGImageView.frame.size.width / 4.0), CGRectGetMinY(_progressBGImageView.frame) + arc4random()%(int)CGRectGetHeight(_progressBGImageView.frame))];
    
    NSValue *p3 = [NSValue valueWithCGPoint:CGPointMake(CGRectGetMinX(_progressBGImageView.frame) + CGRectGetWidth(_progressBGImageView.frame)/2.0 + arc4random()%(int)CGRectGetWidth(_progressBGImageView.frame)/4.0, CGRectGetMinY(_progressBGImageView.frame) + arc4random()%(int)CGRectGetHeight(_progressBGImageView.frame))];
    NSValue *p4 = [NSValue valueWithCGPoint:CGPointMake(CGRectGetMinX(_progressBGImageView.frame) + CGRectGetWidth(_progressBGImageView.frame)/4.0 + arc4random()%(int)CGRectGetWidth(_progressBGImageView.frame)/4.0, CGRectGetMinY(_progressBGImageView.frame) + arc4random()%(int)CGRectGetHeight(_progressBGImageView.frame))];
    NSValue *p5 = [NSValue valueWithCGPoint:CGPointMake(CGRectGetMinY(_progressBGImageView.frame) + 5, CGRectGetMinY(_progressBGImageView.frame) + arc4random()%(int)CGRectGetHeight(_progressBGImageView.frame))];
    keyAnimation.values = @[p1,p2,p3,p4,p5];
    
    CABasicAnimation *rotateAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotateAnimation.fromValue = @0;
    rotateAnimation.toValue = @(M_PI/18.0 * (arc4random()%(36*6)));
    
    CAAnimationGroup *group = [CAAnimationGroup animation];
    group.animations = @[rotateAnimation,keyAnimation];
    group.duration = 8;
    group.delegate = self;
    group.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault];
    [group setValue:leafLayer forKey:@"leafLayer"];
    
    [leafLayer addAnimation:group forKey:nil];
}

-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    CALayer *layer = [anim valueForKey:@"leafLayer"];
    [layer removeFromSuperlayer];
}

@end
